package com.pug.gateway.service.impl;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSON;
import com.pug.gateway.dto.AccessLogReqDTO;
import com.pug.gateway.mapper.AccessLogMapper;
import com.pug.gateway.pojo.AccessLogPO;
import com.pug.gateway.service.IAccessLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @program: xq_pug_alibabacloud_parent
 * @description:
 * @author: Mr.Ren
 * @create: 2023-06-03 18:23
 **/
@Service
@Slf4j
public class AccessLogServiceImpl implements IAccessLogService {

    @Autowired
    private AccessLogMapper accessLogMapper;

    @Override
    public Integer saveLogger(AccessLogReqDTO reqDTO) {
        try {
            AccessLogPO po = new AccessLogPO();
            BeanUtils.copyProperties(reqDTO, po);
            po.setId(IdUtil.getSnowflakeNextId());
            Date date = new Date();
            po.setCreateTime(date);
            po.setUpdateTime(date);
            po.setIsDelete(0);
            log.info("交易网关【统一请求日志入库】接口请求参数:{}", JSON.toJSONString(po));
            int i = accessLogMapper.insert(po);
            if (i > 0) {
                log.info("交易网关【统一请求日志】入库成功");
            } else {
                log.error("交易网关【统一请求日志】入库失败");
            }
            return i;
        } catch (Exception e) {
            log.error("交易网关【统一请求日志】入库失败，原因：" + e);
            return 0;
        }
    }
}
